package com.wanmei.controller.user;


import com.github.pagehelper.PageInfo;
import com.wanmei.pojo.JsonResult;
import com.wanmei.pojo.Role;
import com.wanmei.pojo.User;
import com.wanmei.pojo.UserList;
import com.wanmei.service.RoleService;
import com.wanmei.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class UserListController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    /**
     * 显示用户界面
     */
    @RequestMapping("/listUserDESC.do")
    public ModelAndView listUserDESC(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize, HttpServletRequest req) {
        ModelAndView mv = new ModelAndView();
        String likeValue = req.getParameter("likeValue");
        PageInfo<UserList> pageInfo = userService.getLikeUserInfoDesc(pageNum, pageSize, likeValue);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("user/userList");
        return mv;
    }

    /**
     * 模糊查询
     */
    @RequestMapping("/listUser.do")
    public ModelAndView listUserByCorrect(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize, HttpServletRequest req) {
        ModelAndView mv = new ModelAndView();
        String likeValue = req.getParameter("likeValue");

        PageInfo<UserList> pageInfo = userService.getLikeUserInfo(pageNum, pageSize, likeValue);
        req.setAttribute("likeValue",likeValue);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("user/userList");
        return mv;
    }

    /**
     * 删除用户 从前端获取过来的 单个删除
     */

    @RequiresRoles(value = {"admin","system"},logical = Logical.OR)

    @RequestMapping("/deleteUser.do")
    @ResponseBody
    public JsonResult deleteUserSingle(@Param("uid") int uid) {
        JsonResult result = new JsonResult();
        int code = userService.deleteUserByuid(uid);
        result.setCode(code);
        return result;
    }

    /**
     * 获取编辑框
     *
     * @param uid
     * @return
     */
    @RequestMapping("/get_editInfo.json")
    @ResponseBody
    public UserList getEditInfo(@Param("uid") int uid) {
        return userService.getUserListByuid(uid);
    }

    /**
     * 成功编辑页面
     *
     * @param userList
     * @return
     */
    @RequestMapping("/editInfo.json")
    @ResponseBody
    public JsonResult EditInfo(UserList userList) {
        JsonResult result = new JsonResult();
        int code = userService.updateByUser(userList);
        result.setCode(code);
        return result;
    }

    /**
     * 是否有对用户进行操作的权限。
     */
    @RequestMapping("/hasRoles")
    @ResponseBody
    public JsonResult hasRoles(HttpServletRequest req){
        JsonResult result = new JsonResult();
        String username = req.getParameter("username");
        User user1 = userService.getUserByUserName(username);
        Role roles = roleService.getRoleByRoleId(user1.getRid());
        String roleName = roles.getName();
        if (roleName.equals("admin")||roleName.equals("system")){
            result.setCode(0);
        }else {
            result.setCode(1);
        }
        return  result;
    }

    /**
     * 跳转到此界面
     */
    @RequiresRoles(value = {"system"})
    @RequestMapping("/checkRoles")
    @ResponseBody
    public JsonResult checkRoles(){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode(1);
        return jsonResult;
    }

}
